package com.tcu.order.controller;

import com.tcu.order.entity.Order;
import com.tcu.order.entity.OrderItem;
import com.tcu.order.entity.bo.OrderBo;
import com.tcu.order.entity.vo.OrderVo;
import com.tcu.order.service.IOrderService;
import com.tcu.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    private IOrderService orderService;

    @Autowired
    public void setOrderService(IOrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping("add")
    public R addOrder(@RequestBody OrderVo orderVo){
        List<OrderItem> itemList = orderVo.getItemList();
        Order o = new Order();
        BeanUtils.copyProperties(orderVo,o);
        if(itemList==null){
            orderService.save(o);
        }else {
            orderService.saveOrderWithItems(o,itemList);
        }
        return R.success().data("order", o);
    }

    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfoById(@PathVariable String orderId){
        OrderBo ob =orderService.getOrderInfoById(orderId);
        return R.success().data("order", ob);
    }

    @DeleteMapping("delete/{orderId}")
    public R delete(@PathVariable String orderId){
        orderService.deleteOrder(orderId);
        return R.success();
    }

    @PostMapping("update")
    public R update(@RequestBody OrderVo orderVo){
        Order o = new Order();
        BeanUtils.copyProperties(orderVo,o);
        orderService.updateOrder(o,orderVo.getItemList());
        return R.success();
    }
    @GetMapping("changeStatus/{orderId}/{value}")
    public R changeStatus(@PathVariable String orderId,@PathVariable Integer value){
        orderService.changeStatus(orderId,value);
        return R.success();
    }

    @GetMapping("getChildrenOrder/{orderParentId}")
    public R getChildrenOrder(@PathVariable String orderParentId){
        List<OrderBo> childrenRepairOrder = orderService.getChildrenOrder(orderParentId);
        return R.success().data("childrenRepairOrder", childrenRepairOrder);
    }
    @GetMapping("hasChild/{orderId}")
    public R hasChild(@PathVariable String orderId){
        boolean b = orderService.hasChildren(orderId);
        return R.success().data("isHas",b);
    }

    /**
     * 确认订单完成功能,给维修人员增加业绩
     */
    @GetMapping("endOrder/{orderId}/{workerId}")
    public R endOrder(@PathVariable String orderId,@PathVariable String workerId){
        boolean res=orderService.endOrder(orderId,workerId);
        return res?R.success():R.error();
    }
    /**
     * 维修人员正式接单
     */
    @GetMapping("formalReceive/{orderId}/{workerId}")
    public R formalReceive(@PathVariable String orderId,@PathVariable String workerId){
        orderService.formalReceive(orderId,workerId);
        return R.success();
    }

}
